<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
	</head>
	<body>
		<div id="demotu">
			<img src="images/banner1.jpg" id="img"/>
		</div>
		<script src="jquery-1.11.0.js"></script>
		<script type="text/javascript">
		$(window).load(function() {
			var ImgD = document.getElementById('img');
			  autosize2(ImgD,375,375);
        //ImgD:要放图片的img元素，onload时传参可用this

        //h:img元素的高度，像素

        //w:img元素的宽度，像素
        function autosize2(ImgD,h,w)
        {
            var image=new Image(); 
            image.src=ImgD.src; 
            if (image.width<w && image.height<h)
            {
                ImgD.width=image.width;
                ImgD.height=image.height;
            }
            else
            {
                if (w / h <= image.width / image.height)
                {
                    ImgD.width=w;
                    ImgD.height=w * (image.height / image.width);
                }
                else
                {
                    ImgD.width=h * (image.width / image.height);
                    ImgD.height=h;
                }
            }
            
            //图片居中，IE8有效果，IE9,火狐无效果，请在页面用table居中
            //ImgD.style.paddingLeft = (w + 20 - ImgD.width) / 2;   //20是指padding-left和padding-right距离的和
            //ImgD.style.paddingTop=(h + 20 -ImgD.height) / 2;     //20是指padding-top和padding-bottom距离的和
        }

		 });
</script>
	</body>
</html>
